-- Cocwinbar 的配置
local Filetypes = function()
  local filetype = {
    "folderClosed",
    "folderOpened",
    "folderSymlink",
    "file",
    "fileSymlink",
    "fileHidden",
    "excel",
    "word",
    "ppt",
    "stylus",
    "sass",
    "html",
    "xml",
    "ejs",
    "css",
    "webpack",
    "markdown",
    "json",
    "javascript",
    "javascriptreact",
    "ruby",
    "php",
    "python",
    "coffee",
    "mustache",
    "conf",
    "image",
    "ico",
    "twig",
    "c",
    "h",
    "haskell",
    "lua",
    "java",
    "terminal",
    "ml",
    "diff",
    "sql",
    "clojure",
    "edn",
    "scala",
    "go",
    "dart",
    "firefox",
    "vs",
    "perl",
    "rss",
    "csharp",
    "fsharp",
    "rust",
    "dlang",
    "erlang",
    "elixir",
    "elm",
    "mix",
    "vim",
    "ai",
    "psd",
    "psb",
    "typescript",
    "typescriptreact",
    "julia",
    "puppet",
    "vue",
    "swift",
    "git",
    "bashrc",
    "favicon",
    "docker",
    "gruntfile",
    "gulpfile",
    "dropbox",
    "license",
    "procfile",
    "jquery",
    "angular",
    "backbone",
    "requirejs",
    "materialize",
    "mootools",
    "vagrant",
    "svg",
    "font",
    "text",
    "archive",
    "lock",
  }
  -- 文件类型
  -- 判断文件类型是否是列表中表示的类型, 若不是则不运行 winbar
  local FileType = vim.tbl_contains(filetype, vim.bo.filetype)
  if not FileType then
    vim.opt_local.winbar = nil
    return true
  end
  return false
end


local winbartext = function()
  if Filetypes() then
    return
  end

  -- winbar 的函数信息文本
  local symbol_line = function()
    local icons = require("plugins.Coc.icons");
    local curwin = vim.g.statusline_winid or 0
    local curbuf = vim.api.nvim_win_get_buf(curwin)
    local ok, line = pcall(vim.api.nvim_buf_get_var, curbuf, 'coc_symbol_line')
    local WinbarText = '%#CocSymbolLineFile# ' ..
        icons[vim.bo.filetype] ..
        ' %*%#Normal#%t%*' ..
        '%#CocSymbolLineSeparator# %*'
    local Text = '%#CocSymbolLineFile# ' .. icons[vim.bo.filetype] .. ' %*%#Normal#%t%*'
    return ok and WinbarText .. line or Text
  end

  local WinbarText = symbol_line()

  -- 打印 winbar 信息
  local status_ok, _ = pcall(vim.api.nvim_set_option_value, 'winbar', WinbarText, { scope = 'local' })
  if not status_ok then
    return
  end
end

-- 事件监听, 当符合相应事件时执行 winbartext 函数
local wimbar_augroup = vim.api.nvim_create_augroup("_winbar", { clear = true })
vim.api.nvim_create_autocmd({
    'DirChanged',
    'CursorMoved',
    'BufWinEnter',
    'BufFilePost',
    'InsertEnter',
    'BufWritePost',
    'BufEnter',
  },
  {
    group = wimbar_augroup,
    callback = function()
      winbartext()
    end
  })



-- local WinbarText = '%#CocSymbolLineFile# %{WebDevIconsGetFileTypeSymbol()} %*%t%#CocSymbolLineSeparator# > %*'
-- local Text = '%#CocSymbolLineFile# %{WebDevIconsGetFileTypeSymbol()} %*%t'

-- -- -- winbar 的函数信息文本
-- local symbol_line = function()
--   local curwin = vim.g.statusline_winid or 0
--   local curbuf = vim.api.nvim_win_get_buf(curwin)
--   local ok, line = pcall(vim.api.nvim_buf_get_var, curbuf, 'coc_symbol_line')
--   -- return ok and '  %#StatuslineCocError# %*%t ❯ '..line or '%= %#StatuslineCocError# %*%y %='
--   return ok and '  %#StatuslineCocError# %*%t ❯ '..line or '  %#StatuslineCocError# %*%t'
-- end
-- --
-- vim.o.winbar = symbol_line()

-- winbar theme
-- if vim.g.colors_name == "catppuccin-mocha" then
--   vim.cmd [[
--     highlight CocSymbolLineKeyword       guibg=#cba6f8 guifg=#181826
--     highlight CocSymbolLineStruct        guibg=#cba6f8 guifg=#181826
--     highlight CocSymbolLineModule        guibg=#cba6f8 guifg=#181826
--     highlight CocSymbolLineFile          guibg=#cba6f8 guifg=#181826
--     highlight CocSymbolLineValue         guibg=#89b4fb guifg=#181826
--     highlight CocSymbolLineFunction      guibg=#89b4fb guifg=#181826
--     highlight CocSymbolLineMethod        guibg=#89b4fb guifg=#181826
--     highlight CocSymbolLineFolder        guibg=#89b4fb guifg=#181826
--     highlight CocSymbolLineSnippet       guibg=#89b4fb guifg=#181826
--     highlight CocSymbolLineUnit          guibg=#89b4fb guifg=#181826
--     highlight CocSymbolLineDefault       guibg=#89b4fb guifg=#181826
--     highlight CocSymbolLineConstant      guibg=#fab388 guifg=#181826
--     highlight CocSymbolLineEvent         guibg=#fab388 guifg=#181826
--     highlight CocSymbolLineColor         guibg=#fab388 guifg=#181826
--     highlight CocSymbolLineClass         guibg=#74c7ed guifg=#181826
--     highlight CocSymbolLineConstructor   guibg=#74c7ed guifg=#181826
--     highlight CocSymbolLineInterface     guibg=#f9e2b0 guifg=#181826
--     highlight CocSymbolLineEnum          guibg=#f9e2b0 guifg=#181826
--     highlight CocSymbolLineEnumMember    guibg=#b4beff guifg=#181826
--     highlight CocSymbolLineProperty      guibg=#b4beff guifg=#181826
--     highlight CocSymbolLineField         guibg=#b4beff guifg=#181826
--     highlight CocSymbolLineReference     guibg=#b4beff guifg=#181826
--     highlight CocSymbolLineOperator      guibg=#89dcec guifg=#181826
--     highlight CocSymbolLineTypeParameter guibg=#eba0ad guifg=#181826
--     highlight CocSymbolLineText          guibg=#cdd6f5 guifg=#181826
--     highlight CocSymbolLineVariable      guibg=#f38ba9 guifg=#181826
--     highlight CocSymbolLinePackage       guibg=#cba6f8 guifg=#181826
--     highlight CocSymbolLineKey           guibg=#f2cdce guifg=#181826
--     highlight CocSymbolLineNull          guibg=#f2cdce guifg=#181826
--     highlight CocSymbolLineObject        guibg=#f2cdce guifg=#181826
--     highlight CocSymbolLineArray         guibg=#fab388 guifg=#181826
--     highlight CocSymbolLineNumber        guibg=#fab388 guifg=#181826
--     highlight CocSymbolLineBoolean       guibg=#fab388 guifg=#181826
--     highlight CocSymbolLineString        guibg=#a6e3a2 guifg=#181826
--     highlight CocSymbolLineEllipsis      guibg=#cdd6f5 guifg=#181826
--     highlight CocSymbolNamespace         guibg=#b4beff guifg=#181826
--   ]]
-- end
